//
//  EditInfoViewController.m
//  jiyujiaProgram
//
//  Created by Jame on 15/12/15.
//  Copyright © 2015年 中良 赵. All rights reserved.
//

#import "EditInfoViewController.h"
#import "UserProfileManager.h"

@interface EditInfoViewController () <UIScrollViewDelegate,UITextFieldDelegate,UIGestureRecognizerDelegate,UIPickerViewDataSource,UIPickerViewDelegate,UIActionSheetDelegate,MBProgressHUDDelegate,UIAlertViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (nonatomic,strong) NSDictionary *pickerDictionary;
@property (nonatomic,strong) NSArray *provinceArray;
@property (nonatomic,strong) NSArray *cityArray;
@property (nonatomic,strong) NSArray *townArray;
@property (nonatomic,strong) NSArray *selectedArray;

@property (strong, nonatomic) UIView *maskView;
@property (strong, nonatomic) UIView *pickerBackgroundView;
@property (strong, nonatomic) UIPickerView *pickerView;


@property (nonatomic, copy) NSString *imageId;
@property (nonatomic, copy) NSString *imageUrl;

@end

@implementation EditInfoViewController
{
    
    CDPDatePicker *_datePicker;

    UIScrollView *_scrollView;

    UIView *_signView;
    UIImageView *_headImage;
    UILabel *_nameLabel;
    UITextField *_signInfo;
    
    UIView *_baseInfoView;
    UITextField *_nameInfo;
    UITextField *_birthInfo;
    UILabel *_sexInfo;
    UILabel *_fromInfo;
    UITextField *_proInfo;
    UITextField *_collegeInfo;
    UITextField *_teachYearInfo;
    NSString *_province;
    
    UIView *_teacherInfoView;
    UITextField *_specialClassInfo;
    
    UITextField *_specialWayInfo;
    UILabel *_classWayInfo;
    
    UIActionSheet *_sexSheet;
    UIActionSheet *_classWaySheet;
    UIActionSheet *_photoActionSheet;
    NSData *_imageData;
    
    MBProgressHUD *_HUD;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatNavigationBarWithImage:nil title:@"编辑个人资料"];
    [self creatNavigationBarLeftItemWithLeftTitle:nil LeftImage:ECIMAGENAME(@"back_button.png")];
    [self creatNavigationBarRightItemWithRightTitle:@"保存" RightImage:nil];
    

    UserProfileEntity *user = [[UserProfileManager sharedInstance] getCurUserProfile];
    [self creatScrollView];
    [self creatSignView];
    [self creatBaseInfoView];
    [self creatTeacherInfoView];
    
    [self initData];
    [self creatPickerView];
//    [self creatDatePicker];

}


- (void)showHUD
{
    _HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    _HUD.delegate = self;
    _HUD.mode = MBProgressHUDModeDeterminate;
}


- (void)leftBtnClick:(id)leftSender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)rightBtnClick:(id)rightSender
{
    
    if (_nameInfo.text.length == 0) {
        SHOW_ALERT(@"提示", @"请填写姓名", @"好的", nil, nil);
    }else if (_birthInfo.text.length == 0){
        SHOW_ALERT(@"提示", @"请选择生日", @"好的", nil, nil);
    }else if (_sexInfo.text.length == 0){
        SHOW_ALERT(@"提示", @"请选择性别", @"好的", nil, nil);
    }else if (_fromInfo.text.length == 0){
        SHOW_ALERT(@"提示", @"请选择城市", @"好的", nil, nil);
    }else if (_collegeInfo.text.length == 0){
        SHOW_ALERT(@"提示", @"请填写学校", @"好的", nil, nil);
    }else if (_signInfo.text.length == 0){
        SHOW_ALERT(@"提示", @"请填写签名", @"好的", nil, nil);
    }else if (_proInfo.text.length == 0){
        SHOW_ALERT(@"提示", @"请填写专业", @"好的", nil, nil);
    }else if (_teachYearInfo.text.length == 0){
        SHOW_ALERT(@"提示", @"请填写教龄", @"好的", nil, nil);
    }else if (_classWayInfo.text.length == 0){
        SHOW_ALERT(@"提示", @"请选择上课方式", @"好的", nil, nil);
    }else{
        [self showHUD];
        NSDictionary *firstParam = @{}.mutableCopy;
        NSDictionary *secondParam = @{}.mutableCopy;
        firstParam = @{
                       @"member_Id":self.memberId,
                       @"name":_nameInfo.text,
                       @"age":_birthInfo.text,
                       @"sex":_sexInfo.text,
                       @"city":_fromInfo.text,
                       @"country":@"中国",
                       @"province":_province,
                       @"area":_fromInfo.text,
                       @"school":_collegeInfo.text,
                       @"signature":_signInfo.text,
                       @"imgId":self.imageId,
                       @"imgUrl":self.imageUrl,
                       };
        secondParam = @{
                        @"member_Id":self.memberId,
                        @"specialty":_proInfo.text,
                        @"seniority":_teachYearInfo.text,
                        @"special_programs":_specialClassInfo.text,
                        @"teaching_skills":_specialWayInfo.text,
                        @"have_lessons_way":_classWayInfo.text,
                        };
        
        [[HttpManager defaultManager] postRequestToUrl:RequestURL(UPDATEMINE) params:firstParam complete:^(BOOL successed, NSDictionary *result) {
            if (successed) {
                if ([result[@"errCode"] integerValue] == 200) {
                    
                    [[HttpManager defaultManager] postRequestToUrl:RequestURL(UPDATEMINEINFO) params:secondParam complete:^(BOOL successed, NSDictionary *result) {
                        if (successed) {
                            if ([result[@"errCode"] integerValue] == 200) {
                                [_HUD hide:YES];
                                SHOW_ALERT(@"提示", result[@"errMsg"], @"好的", nil, self);
                            }else{
                                [_HUD hide:YES];
                                SHOW_ALERT(@"提示", result[@"errMsg"], @"好的", nil, nil);
                            }
                        }else{
                            [_HUD hide:YES afterDelay:.5f];
                        }
                    }];
                    
                }else{
                    [_HUD hide:YES];
                    SHOW_ALERT(@"提示", result[@"errMsg"], @"好的", nil, nil);
                }
            }else{
                [_HUD hide:YES afterDelay:.5f];
            }
        }];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}


- (void)creatScrollView
{
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, ECSCREEN_W, ECSCREEN_H - 64)];
    _scrollView.delegate = self;
    _scrollView.contentSize = CGSizeMake(ECSCREEN_W, 455);
    _scrollView.backgroundColor = BG_COLOR;
    _scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_scrollView];
    
    UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchScrollView)];
    [recognizer setNumberOfTapsRequired:1];
    [recognizer setNumberOfTouchesRequired:1];
    recognizer.delegate = self;
    [_scrollView addGestureRecognizer:recognizer];

}

- (void)touchScrollView
{
    [_signInfo resignFirstResponder];
    [_nameInfo resignFirstResponder];
    [_proInfo resignFirstResponder];
    [_collegeInfo resignFirstResponder];
    [_teachYearInfo resignFirstResponder];
    [_specialClassInfo resignFirstResponder];
    [_specialWayInfo resignFirstResponder];
    [_birthInfo resignFirstResponder];
}


- (void)creatSignView
{
    _signView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ECSCREEN_W, 90)];
    _signView.backgroundColor = BG_COLOR ;
//    _signView.userInteractionEnabled = YES;
    [_scrollView addSubview:_signView];
    
    
    UIImageView *headBg = [[UIImageView alloc] initWithFrame:CGRectMake(20, 5, 50, 50)];
    headBg.backgroundColor = ECCOLOR(255, 255, 255, 1);
    headBg.clipsToBounds = YES;
    headBg.layer.cornerRadius = 1;
    [_signView addSubview:headBg];
    
    _headImage = [[UIImageView alloc] initWithFrame:CGRectMake(1, 1, 48, 48)];
    [_headImage sd_setImageWithURL:[NSURL URLWithString:self.headUrl] placeholderImage:ECIMAGENAME(@"header.png")];
    _headImage.clipsToBounds = YES;
    _headImage.layer.cornerRadius = 1;
    [headBg addSubview:_headImage];
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(15, 5, 60, 50);
    [button addTarget:self action:@selector(headerClick:) forControlEvents:UIControlEventTouchUpInside];
    [_signView addSubview:button];
    
    _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 30, ECSCREEN_W - 80 -  100, 25)];
    _nameLabel.text = self.name;
    _nameLabel.font = [UIFont systemFontOfSize:15.f];
    _nameLabel.textColor = NAVC_COLOR;
    [_signView addSubview:_nameLabel];
    
    
    UILabel *signTitle = [[UILabel alloc] initWithFrame:CGRectMake(20, 60, 50, 30)];
    signTitle.text = @"签名:";
    signTitle.font = [UIFont systemFontOfSize:15.f];
    [_signView addSubview:signTitle];
    
    
#warning *********SIGNTEXTFIELD*********
    _signInfo = [[UITextField alloc] initWithFrame:CGRectMake(70, 60, ECSCREEN_W - 80 - 20 - 10 , 30)];
    _signInfo.text = self.sign;
    _signInfo.delegate = self;
//    _signInfo.backgroundColor = [UIColor redColor];
    _signInfo.font = [UIFont systemFontOfSize:11.f];
    [_signView addSubview:_signInfo];
    
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(ECSCREEN_W - 40, 65, 20, 20)];
    image.image = ECIMAGENAME(@"right_to_edit.png");
    [_signView addSubview:image];
}


- (void)creatBaseInfoView
{
    _baseInfoView = [[UIView alloc] initWithFrame:CGRectMake(0, 90, ECSCREEN_W, 220)];
    _baseInfoView.backgroundColor = BG_COLOR;
    [_scrollView addSubview:_baseInfoView];
    
    
    UIView *bg = [[UIView alloc] initWithFrame:CGRectMake(10, 10, ECSCREEN_W - 20, 200)];
    bg.backgroundColor = ECCOLOR(255, 255, 255, 1);
    bg.clipsToBounds = YES;
    bg.layer.cornerRadius = 3;
    [_baseInfoView addSubview:bg];
    
    //title
    NSArray *titleArr = @[@"姓名:",@"年龄:",@"性别:",@"来自:",@"专业:",@"毕业院校:",@"教龄:"];
    for (int i = 0; i < titleArr.count; i ++) {
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, i * 28 + 2, 75, 28)];
        titleLabel.text = titleArr[i];
        titleLabel.textAlignment = NSTextAlignmentRight;
        titleLabel.textColor = ECCOLOR(0, 0, 0, 1);
        titleLabel.font = [UIFont systemFontOfSize:13.f];
        [bg addSubview:titleLabel];
        
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(ECSCREEN_W - 40 - 10, i * 28 + 2 + 4, 20, 20)];
        image.image = ECIMAGENAME(@"right_to_edit.png");
        [bg addSubview:image];
        
        if(i+1 < titleArr.count){
            UIView *line = [[UIView alloc] initWithFrame:CGRectMake(10, (i + 1)*28, ECSCREEN_W - 40, 1)];
            line.backgroundColor = ECCOLOR(0, 0, 0, .3f);
            [bg addSubview:line];
        }
    }
    
    
    _nameInfo = [[UITextField alloc] initWithFrame:CGRectMake(80, 2, ECSCREEN_W - 40 - 80 - 10, 28)];
    _nameInfo.text = self.name;
    _nameInfo.delegate = self;
    _nameInfo.font = [UIFont systemFontOfSize:13.f];
    [bg addSubview:_nameInfo];
    
    _birthInfo = [[UITextField alloc] initWithFrame:CGRectMake(80, 30, ECSCREEN_W - 40 - 80 - 10, 28)];
    _birthInfo.text = self.birthday;
    _birthInfo.delegate = self;
    _birthInfo.font = [UIFont systemFontOfSize:13.f];
    [bg addSubview:_birthInfo];
    
//    UIButton *birButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    birButton.frame = CGRectMake(80, 30, ECSCREEN_W - 40 - 80, 28);
//    [birButton addTarget:self action:@selector(birthdayClick:) forControlEvents:UIControlEventTouchUpInside];
//    [bg addSubview:birButton];
    
    _sexInfo = [[UILabel alloc] initWithFrame:CGRectMake(80, 58, ECSCREEN_W - 40 - 80 - 10, 28)];
    _sexInfo.text = self.sex;
    _sexInfo.font = [UIFont systemFontOfSize:13.f];
    [bg addSubview:_sexInfo];
    
    
    UIButton *sexButton = [UIButton buttonWithType:UIButtonTypeCustom];
    sexButton.frame = CGRectMake(80, 58, ECSCREEN_W - 40 - 80, 28);
    [sexButton addTarget:self action:@selector(sexClick:) forControlEvents:UIControlEventTouchUpInside];
    [bg addSubview:sexButton];
    
    _fromInfo = [[UILabel alloc] initWithFrame:CGRectMake(80, 86, ECSCREEN_W - 40 - 80 - 10, 28)];
    _fromInfo.text = self.from;
    _fromInfo.font = [UIFont systemFontOfSize:13.f];
    [bg addSubview:_fromInfo];
    
    
    UIButton *fromButton = [UIButton buttonWithType:UIButtonTypeCustom];
    fromButton.frame = CGRectMake(80, 86, ECSCREEN_W - 40 - 80, 28);
    [fromButton addTarget:self action:@selector(fromClick:) forControlEvents:UIControlEventTouchUpInside];
    [bg addSubview:fromButton];
    
    _proInfo = [[UITextField alloc] initWithFrame:CGRectMake(80, 114, ECSCREEN_W - 40 - 80 - 10, 28)];
    _proInfo.text = self.pro;
    _proInfo.delegate = self;
    _proInfo.font = [UIFont systemFontOfSize:13.f];
    [bg addSubview:_proInfo];
    
    _collegeInfo = [[UITextField alloc] initWithFrame:CGRectMake(80, 142, ECSCREEN_W - 40 - 80 - 10, 28)];
    _collegeInfo.text = self.college;
    _collegeInfo.delegate = self;
    _collegeInfo.font = [UIFont systemFontOfSize:13.f];
    [bg addSubview:_collegeInfo];
    
    
    _teachYearInfo = [[UITextField alloc] initWithFrame:CGRectMake(80, 170, ECSCREEN_W - 40 - 80 - 10, 28)];
    _teachYearInfo.text = self.teachYear;
    _teachYearInfo.delegate = self;
    _teachYearInfo.font = [UIFont systemFontOfSize:13.f];
    [bg addSubview:_teachYearInfo];
}


- (void)creatTeacherInfoView
{
    _teacherInfoView = [[UIView alloc] initWithFrame:CGRectMake(0, 310, ECSCREEN_W, 140)];
    _teacherInfoView.backgroundColor = BG_COLOR;
    [_scrollView addSubview:_teacherInfoView];
    

    UIView *bg = [[UIView alloc] initWithFrame:CGRectMake(10, 10, ECSCREEN_W - 20, 120)];
    bg.backgroundColor = ECCOLOR(255, 255, 255, 1);
    bg.clipsToBounds = YES;
    bg.layer.cornerRadius = 3;
    [_teacherInfoView addSubview:bg];
    
    
    for (int i = 0; i < 3; i++) {
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(ECSCREEN_W - 40 - 10, i * 40 + 10, 20, 20)];
        image.image = ECIMAGENAME(@"right_to_edit.png");
        [bg addSubview:image];
    }

    
    //特色课程
    UIImageView *specialImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 20, 20)];
    specialImage.image = ECIMAGENAME(@"special.png");
    [bg addSubview:specialImage];
    
    
    UILabel *specialTitle = [[UILabel alloc] initWithFrame:CGRectMake(35, 6, 60, 28)];
    specialTitle.text = @"特色课程:";
    specialTitle.font = [UIFont systemFontOfSize:13.f];
    [bg addSubview:specialTitle];
    
    
    _specialClassInfo = [[UITextField alloc] initWithFrame:CGRectMake(95, 6, ECSCREEN_W - 40 - 95 - 10, 28)];
    _specialClassInfo.text = self.specialClass;
    _specialClassInfo.delegate = self;
    _specialClassInfo.font = [UIFont systemFontOfSize:13.f];
    //    _specialInfo.backgroundColor = [UIColor redColor];
    [bg addSubview:_specialClassInfo];
    
    
    
    UIView *firstLine = [[UIView alloc] initWithFrame:CGRectMake(10, 39, ECSCREEN_W - 40, 1)];
    firstLine.backgroundColor = ECCOLOR(0, 0, 0, .3f);
    [bg addSubview:firstLine];
    
    //特长
    UIImageView *eduspecialImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 50, 20, 20)];
    eduspecialImage.image = ECIMAGENAME(@"edu_special.png");
    [bg addSubview:eduspecialImage];
    
    
    UILabel *eduspecialTitle = [[UILabel alloc] initWithFrame:CGRectMake(35, 50, 60, 20)];
    eduspecialTitle.text = @"教学特长:";
    eduspecialTitle.font = [UIFont systemFontOfSize:13.f];
    [bg addSubview:eduspecialTitle];
    
    _specialWayInfo = [[UITextField alloc] initWithFrame:CGRectMake(95, 50, ECSCREEN_W - 40 - 95 - 10, 20)];
    _specialWayInfo.text = self.specialWay;
    _specialWayInfo.delegate = self;
    _specialWayInfo.font = [UIFont systemFontOfSize:13.f];
    [bg addSubview:_specialWayInfo];
    
    
    UIView *secondLine = [[UIView alloc] initWithFrame:CGRectMake(10, 79, ECSCREEN_W - 40, 1)];
    secondLine.backgroundColor = ECCOLOR(0, 0, 0, .3);
    [bg addSubview:secondLine];
    
    
    UIImageView *eduwayImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 90, 20, 20)];
    eduwayImage.image = ECIMAGENAME(@"edu_way.png");
    [bg addSubview:eduwayImage];
    
    
    UILabel *eduwayTitle = [[UILabel alloc] initWithFrame:CGRectMake(35, 90, 60, 20)];
    eduwayTitle.text = @"上课方式:";
    eduwayTitle.font = [UIFont systemFontOfSize:13.f];
    [bg addSubview:eduwayTitle];
    
    
    _classWayInfo = [[UILabel alloc] initWithFrame:CGRectMake(95, 90, ECSCREEN_W - 40 - 95 - 10, 20)];
    _classWayInfo.text = self.classWay;
    _classWayInfo.font = [UIFont systemFontOfSize:13.f];
    [bg addSubview:_classWayInfo];
    
    
    UIButton *classWayButton = [UIButton buttonWithType:UIButtonTypeCustom];
    classWayButton.frame = CGRectMake(95, 90, ECSCREEN_W - 40 - 95, 20);
    [classWayButton addTarget:self action:@selector(classWayClick:) forControlEvents:UIControlEventTouchUpInside];
    [bg  addSubview:classWayButton];
    
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}



//#pragma mark - creatDatePicker
//- (void)creatDatePicker
//{
//    _datePicker=[[CDPDatePicker alloc] initWithView:self.view delegate:self];
//    _datePicker.theTypeOfDatePicker = 2;
//}
//
//
//#pragma mark - CDPDatePickerDidConfirm
////回调，字符串可自行进行截取
//-(void)CDPDatePickerDidConfirm:(NSString *)confirmString
//{
//    _birthInfo.text = confirmString;
//}



#pragma mark - 初始化地理信息
- (void)initData
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Address" ofType:@"plist"];
    self.pickerDictionary = [[NSDictionary alloc] initWithContentsOfFile:path];
    //获取省信息
    self.provinceArray = [self.pickerDictionary allKeys];
    self.selectedArray = [self.pickerDictionary objectForKey:[[self.pickerDictionary allKeys] objectAtIndex:0]];
    //获取市信息
    if (self.selectedArray.count > 0) {
        self.cityArray = [[self.selectedArray objectAtIndex:0] allKeys];
    }
    //获取区、镇信息
    if (self.cityArray.count > 0) {
        self.townArray = [[self.selectedArray objectAtIndex:0] objectForKey:[self.cityArray objectAtIndex:0]];
        
        //NSLog(@"省%@市%@县%@",self.provinceArray,self.cityArray,self.townArray);
    }
    
    self.imageId = @"";
    self.imageUrl = @"";
}


#pragma mark - 显示PickerView
- (void)showPicker
{
    [self.view addSubview:self.maskView];
    [self.view addSubview:self.pickerBackgroundView];
    self.maskView.alpha = 0;
    //    self.pickerBackgroundView.top = self.view.height;
    
    [UIView animateWithDuration:0.3 animations:^{
        self.maskView.alpha = 0.3;
        self.pickerBackgroundView.frame=CGRectMake(0,ECSCREEN_H-256,ECSCREEN_W,256);
    }];
}

#pragma mark - 隐藏PickerView
- (void)hidePicker
{
    [UIView animateWithDuration:0.3 animations:^{
        self.maskView.alpha = 0;
        self.pickerBackgroundView.top = self.view.height;
    } completion:^(BOOL finished) {
        [self.maskView removeFromSuperview];
        [self.pickerBackgroundView removeFromSuperview];
    }];
}

#pragma mark - 创建PickerView
- (void)creatPickerView
{
    //pickerView背景
    self.pickerBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0,ECSCREEN_H,ECSCREEN_W,256)];
    self.pickerBackgroundView.backgroundColor = ECCOLOR(0, 0, 0, .8f);
    
    //确定按钮
    UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
    confirmButton.frame = CGRectMake(self.pickerBackgroundView.frame.size.width/2,0,self.pickerBackgroundView.frame.size.width/2,40);
    confirmButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    confirmButton.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 10);
    [confirmButton setTitle:@"确定" forState:UIControlStateNormal];
    [confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    confirmButton.titleLabel.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:15];
    confirmButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    [confirmButton addTarget:self action:@selector(confirmButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.pickerBackgroundView addSubview:confirmButton];
    
    
    //取消按钮
    UIButton *cancleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    cancleButton.frame = CGRectMake(0,0,self.pickerBackgroundView.frame.size.width/2,40);
    cancleButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    cancleButton.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    [cancleButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancleButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    cancleButton.titleLabel.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:15];
    cancleButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    [cancleButton addTarget:self action:@selector(cancleButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.pickerBackgroundView addSubview:cancleButton];
    
    //pickerView
    self.pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,40,self.pickerBackgroundView.frame.size.width,216)];
    self.pickerView.delegate = self;
    self.pickerView.backgroundColor = [UIColor whiteColor];
    [self.pickerBackgroundView addSubview:self.pickerView];
}

#pragma mark - UIPickerViewDelegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}


- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0) {
        return self.provinceArray.count;
    }else if(component == 1){
        return  self.cityArray.count;
    }else{
        return self.townArray.count;
    }
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    
    float width = ECSCREEN_W/3;
    UILabel *label = (UILabel *)view;
    
    if (component == 0) {
        
        if (nil == view) {
            label = [[UILabel alloc]initWithFrame:CGRectMake(0.f, 0.f, width, 30)];
        }
        
        label.text = [self.provinceArray objectAtIndex:row];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:15.f];
    }else if (component == 1){
        
        if (nil == view) {
            label = [[UILabel alloc]initWithFrame:CGRectMake(0.f, 0.f, width, 30)];
        }
        
        label.text = [self.cityArray objectAtIndex:row];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:15.f];
    }else{
        
        if (nil == view) {
            label = [[UILabel alloc]initWithFrame:CGRectMake(0.f, 0.f, 0, 30)];
        }
        
        label.text = [self.townArray objectAtIndex:row];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:15.f];
    }
    return label;
}



- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    if (pickerView == self.pickerView) {
        if (component == 0) {
            return ECSCREEN_W/3;
        } else if (component == 1) {
            return ECSCREEN_W/3*2;
        } else {
            return 110;
        }
    }
    return 0;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component == 0) {
        self.selectedArray = [self.pickerDictionary objectForKey:[self.provinceArray objectAtIndex:row]];
        if (self.selectedArray.count > 0) {
            self.cityArray = [[self.selectedArray objectAtIndex:0] allKeys];
        } else {
            self.cityArray = nil;
        }
        if (self.cityArray.count > 0) {
            self.townArray = [[self.selectedArray objectAtIndex:0] objectForKey:[self.cityArray objectAtIndex:0]];
        } else {
            self.townArray = nil;
        }
    }
    [self.pickerView selectedRowInComponent:1];
    [self.pickerView reloadComponent:1];
    //    [self.pickerView selectedRowInComponent:2];
    
    if (component == 1) {
        if (self.selectedArray.count > 0 && self.cityArray.count > 0) {
            self.townArray = [[self.selectedArray objectAtIndex:0] objectForKey:[self.cityArray objectAtIndex:row]];
        } else {
            self.townArray = nil;
        }
        //        [self.pickerView selectRow:1 inComponent:2 animated:YES];
    }
    //    [self.pickerView reloadComponent:2];
}

#pragma mark - 确定按钮触发事件
- (void)confirmButtonClick:(UIButton *)confirmButton
{
    
    if (_province) {
        _province = @"";
    }else{
        _province = [[NSString alloc] init];
    }
    
    NSString *city = [self.cityArray objectAtIndex:[self.pickerView  selectedRowInComponent:1]];
    _fromInfo.text = city;
    _province = [self.provinceArray objectAtIndex:[self.pickerView selectedRowInComponent:0]];
    
    NSLog(@"%@",[self.provinceArray objectAtIndex:[self.pickerView selectedRowInComponent:0]]);
    NSLog(@"%@",[self.cityArray objectAtIndex:[self.pickerView selectedRowInComponent:1]]);
    //    NSLog(@"%@",[self.townArray objectAtIndex:[self.pickerView selectedRowInComponent:2]]);
    
    [self hidePicker];
}


#pragma mark - 取消按钮触发事件
- (void)cancleButtonClick:(UIButton *)cancleButton
{
    [self hidePicker];
}


- (void)hudWasHidden:(MBProgressHUD *)hud
{
    _HUD = nil;
    [_HUD removeFromSuperview];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - ACTION

- (void)headerClick:(UIButton *)button
{
    [_signInfo resignFirstResponder];
    [_nameInfo resignFirstResponder];
    [_proInfo resignFirstResponder];
    [_collegeInfo resignFirstResponder];
    [_teachYearInfo resignFirstResponder];
    [_specialClassInfo resignFirstResponder];
    [_specialWayInfo resignFirstResponder];
    [_birthInfo resignFirstResponder];

    NSLog(@"HEADER");
    _photoActionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"我的相册", @"相机",nil];
    [_photoActionSheet showInView:self.view];

}


- (void)birthdayClick:(UIButton *)button
{
    [_signInfo resignFirstResponder];
    [_nameInfo resignFirstResponder];
    [_proInfo resignFirstResponder];
    [_collegeInfo resignFirstResponder];
    [_teachYearInfo resignFirstResponder];
    [_specialClassInfo resignFirstResponder];
    [_specialWayInfo resignFirstResponder];
    [_birthInfo resignFirstResponder];

    NSLog(@"BIRTHDAY");
    [_datePicker pushDatePicker];
}


- (void)sexClick:(UIButton *)button
{
    [_signInfo resignFirstResponder];
    [_nameInfo resignFirstResponder];
    [_proInfo resignFirstResponder];
    [_collegeInfo resignFirstResponder];
    [_teachYearInfo resignFirstResponder];
    [_specialClassInfo resignFirstResponder];
    [_specialWayInfo resignFirstResponder];
    [_birthInfo resignFirstResponder];

    NSLog(@"SEX");
    _sexSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"男", @"女",nil];
    [_sexSheet showInView:self.view];
}


- (void)fromClick:(UIButton *)button
{
    [_signInfo resignFirstResponder];
    [_nameInfo resignFirstResponder];
    [_proInfo resignFirstResponder];
    [_collegeInfo resignFirstResponder];
    [_teachYearInfo resignFirstResponder];
    [_specialClassInfo resignFirstResponder];
    [_specialWayInfo resignFirstResponder];
    [_birthInfo resignFirstResponder];

    NSLog(@"FROM");
    
    self.maskView = [[UIView alloc] initWithFrame:ECSCREEN];
    self.maskView.backgroundColor = [UIColor blackColor];
    self.maskView.alpha = 0;
    [self.maskView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hidePicker)]];
    
    [self showPicker];
}


- (void)classWayClick:(UIButton *)button
{
    [_signInfo resignFirstResponder];
    [_nameInfo resignFirstResponder];
    [_proInfo resignFirstResponder];
    [_collegeInfo resignFirstResponder];
    [_teachYearInfo resignFirstResponder];
    [_specialClassInfo resignFirstResponder];
    [_specialWayInfo resignFirstResponder];
    [_birthInfo resignFirstResponder];

    NSLog(@"CLASSWAY");
    _classWaySheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"老师上门", @"学生上门",@"双方协商",@"在线上课",nil];
    [_classWaySheet showInView:self.view];
}


- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (actionSheet == _sexSheet) {
        if ([[_sexSheet buttonTitleAtIndex:buttonIndex] isEqualToString:@"取消"]) {
            _sexInfo.text = @"";
        }else{
            _sexInfo.text = [_sexSheet buttonTitleAtIndex:buttonIndex];
        }
    }else if (actionSheet == _photoActionSheet){
        switch (buttonIndex)
        {
            case 0:
            {
                if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
                {
                    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
                    picker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
                    picker.delegate = self;
                    picker.allowsEditing = YES;
                    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                    [self presentViewController:picker animated:YES completion:nil];
                }
            }
                break;
                
            case 1:
            {
                if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
                {
                    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
                    picker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
                    picker.delegate = self;
                    picker.allowsEditing = YES;
                    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
                    [self presentViewController:picker animated:YES completion:nil];
                }
            }
                break;
                
            default:
                break;
        }
    }else{
        if ([[_classWaySheet buttonTitleAtIndex:buttonIndex] isEqualToString:@"取消"]) {
            _classWayInfo.text = @"";
        }else{
            _classWayInfo.text = [_classWaySheet buttonTitleAtIndex:buttonIndex];
        }
    }
}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    _headImage.image = image;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
    
    dispatch_async(queue, ^{
        
        if (picker.sourceType == UIImagePickerControllerSourceTypeCamera)
        {
            UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
        }
        
        if (UIImagePNGRepresentation(image))
        {
            UIImage *newImage = [ECHelper imageWithImageSimple:image scaledToSize:CGSizeMake(320.0f, 320.0f)];
            _imageData = UIImagePNGRepresentation(newImage);
        }
        else
        {
            UIImage *newImage = [ECHelper imageWithImageSimple:image scaledToSize:CGSizeMake(320.0f, 320.0f)];
            _imageData = UIImageJPEGRepresentation(newImage, 0.75);
        }
        
    });
    
#warning ******************POSTPIC********************
    [self dismissViewControllerAnimated:YES completion:^{
        
        NSString *strRandomName = [NSString stringWithFormat:@"iOS_uploadImage_%d.jpg",arc4random_uniform(10000)];
        
        NSArray *files = @[
                           @{
                               @"name": strRandomName,
                             @"file": _imageData ,
                             @"key": @"image",
                             @"type": @"multipart/form-data"
                             }
                           ];
        
        [[HttpManager defaultManager] uploadToUrl:RequestURL(POSTPIC) params:nil files:files complete:^(BOOL successed, NSDictionary *result) {
            if (successed) {
                if ([result[@"errCode"] integerValue] == 0) {
                    self.imageId = result[@"pictures_ID"];
                    self.imageUrl = result[@"path"];
                }else{
                    SHOW_ALERT(@"提示", result[@"errMsg"], @"好的", nil, nil);
                }
            }
        }];
    }];
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
